package com.csii.pos.activiti.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.info.user.UserPo;
import com.csii.adhoc.info.user.service.UserService;
import com.csii.pos.activiti.mapper.ActPromtptMapper;
import com.csii.pos.activiti.mapper.ActivitiUofPlanMapper;
import com.csii.pos.activiti.service.ActPromptService;
import com.csii.pos.activiti.vo.ApprovalPromptVo;
import com.csii.pos.uofplan.UofPlanPo;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @ClassName: ActPromptServiceImpl
 * @Author: zhanghao
 * @description: TODO
 * @Date: 2021/9/9 10:20
 * @Version: 1.0
 */
@Service
public class ActPromptServiceImpl implements ActPromptService {

    @Autowired
    private ActPromtptMapper actPromtptMapper;
    @Autowired
    private UserService userService;

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    @Override
    public List<ApprovalPromptVo> getPromptMsg(Map<String, Object> map) {
        //获取当前用户登录时输入的用户名,是唯一的.
        String userName = Optional.ofNullable((String)SecurityUtils.getSubject().getPrincipal()).orElse("未知用户!");

        //通过用户名查询当前登录用户信息,并获取其所有角色
        String role = userService.selectUserByUserName(userName).getRole();

        if (null == role || "".equals(role))
            throw new ParamValidityException("当前用户没用任何角色!");

        //用于存储当前用户所有要审批信息
        List<ApprovalPromptVo> mesList = new ArrayList<>();

        //168576677838848--部门领导角色id
        if (role.contains("168576677838848")) {
            if (null == map.get("uofPlanItlNo") || "".equals(map.get("uofPlanItlNo")))
                throw new ParamValidityException("部门审批时部门编号不能为空!");
            //先查部门领导总的审批数据
            List<Task> taskList = taskService.createTaskQuery().processDefinitionKey("approve_uofPlan").taskAssignee("部门领导").list();
            List<String> ids = new ArrayList<>();
            //遍历获取主键id
            if (taskList.size() != 0) {
                for (Task task : taskList) {
                    String processInstanceId = task.getProcessInstanceId();
                    String businessKey = runtimeService.createProcessInstanceQuery().processDefinitionKey("approve_uofPlan")
                            .processInstanceId(processInstanceId).singleResult().getBusinessKey();
                    //businessKey的格式为uofPlan_id , 遍历截取id
                    ids.add(businessKey.split("_")[1]);
                }
                //根据根据主键和部门编号查询当前部门领导需要审批的总数
                Long deptCount = actPromtptMapper.getCount(ids, map);
                //设置审批提示对象数据
                if (0 != deptCount && null != deptCount) {
                    ApprovalPromptVo promptVo1 = new ApprovalPromptVo();
                    promptVo1.setApprovalType("部门审批")
                            .setGoToUrl("DepartApproval")
                            .setMsgCount(deptCount);
                    mesList.add(promptVo1);
                }
            }
        }
        //168576753336320--分管领导角色id
        if (role.contains("168576753336320")) {
            Long count = getApproveCount("approve_uofPlan", "分管领导");
            //设置审批提示对象数据
            if (0 != count && null != count) {
                ApprovalPromptVo promptVo2 = new ApprovalPromptVo();
                promptVo2.setApprovalType("分管审批")
                        .setGoToUrl("LeaderApproval")
                        .setMsgCount(count);
                mesList.add(promptVo2);
            }

        }
        //168576812056576--总经理角色id
        if (role.contains("168576812056576")) {
            Long count = getApproveCount("approve_uofPlan", "总经理");
            //设置审批提示对象数据
            if (0 != count && null != count) {
                ApprovalPromptVo promptVo2 = new ApprovalPromptVo();
                promptVo2.setApprovalType("总经理审批")
                        .setGoToUrl("ManagerApproval")
                        .setMsgCount(count);
                mesList.add(promptVo2);
            }
        }

        //168657535631360--投融资审批员角色id
        if (role.contains("168657535631360")) {
            Long count = getApproveCount("approve_scalePlan", "审批员");
            //设置审批提示对象数据
            if (0 != count && null != count) {
                ApprovalPromptVo promptVo2 = new ApprovalPromptVo();
                promptVo2.setApprovalType("投融资审批")
                        .setGoToUrl("IfScalePlanApproval")
                        .setMsgCount(count);
                mesList.add(promptVo2);
            }

        }
        return mesList;
    }

    public Long getApproveCount(String definitionKey, String taskAssignee) {
        Long count = taskService.createTaskQuery().processDefinitionKey(definitionKey).taskAssignee(taskAssignee).count();
        return count;
    }
}
